home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / cgrid.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  13.7 KB  |  497 lines

  1. //---------------------------------------------------------------------------
  2. // Borland C++Builder
  3. // Copyright (c) 1987, 1998 Borland International Inc.  All Rights Reserved.
  4. //---------------------------------------------------------------------------
  5. #include  "cgrid.h"
  6. #include "samp.h"
  7. #pragma resource "*.res"   
  8. #pragma package(smart_init)
  9.  
  10. namespace Cgrid {
  11. void __fastcall PACKAGE Register()
  12. {
  13.   TComponentClass classes[1] = {__classid(TCColorGrid)};
  14.   RegisterComponents(LoadStr(Tab_101), classes, 0);
  15. }
  16. }
  17.  
  18. __fastcall TCColorGrid::TCColorGrid(TComponent *AOwner):
  19.          TCustomControl( AOwner )
  20. {
  21.   ControlStyle = ControlStyle;// + [csOpaque]; // cbf
  22.   FGridOrdering = go4x4;
  23.   FNumXSquares = 4;
  24.   FNumYSquares = 4;
  25.   FForegroundEnabled = True;
  26.   FBackgroundEnabled = True;
  27.   Color = TColor(clBtnFace);   // cbf assinging int to Tcolor
  28.   Canvas->Brush->Style = bsSolid;
  29.   Canvas->Pen->Color = clBlack;
  30.   SetBounds(0, 0, 100, 100);
  31.   GetPaletteEntries(GetStockObject(DEFAULT_PALETTE), 0, NumPaletteEntries,
  32.   FPaletteEntries);
  33. }
  34.  
  35. void __fastcall TCColorGrid::CreateWnd(void)
  36. {
  37.    TCustomControl::CreateWnd();
  38.    SetWindowLong(Handle, GWL_STYLE, GetWindowLong(Handle, GWL_STYLE) | WS_CLIPSIBLINGS);
  39. }
  40.  
  41. //class TempRect
  42. void __fastcall TCColorGrid::DrawSquare(Integer Which, Boolean ShowSelector)
  43. {
  44.   Integer WinTop, WinLeft;
  45.   Integer PalIndex;
  46.   TRect CellRect;
  47.   if( Which >=0 && Which <= 15 )
  48.   {
  49.     if( Which < 8 )
  50.       PalIndex = Which;
  51.     else
  52.       PalIndex = Which + 4;
  53.     WinTop = ( Which/FNumXSquares ) * FCellYSize;
  54.     WinLeft = (Which % FNumXSquares) * FCellXSize;
  55.     CellRect = Bounds(WinLeft, WinTop, FCellXSize, FCellYSize);
  56.     if( Ctl3D ) {
  57.       Canvas->Pen->Color = TColor(clBtnFace);
  58.       Canvas->Rectangle(RECT(CellRect).left,
  59.                         RECT(CellRect).top,
  60.                         RECT(CellRect).right,
  61.                         RECT(CellRect).bottom);
  62.       InflateRect(&RECT(CellRect), -1, -1);
  63.       Extctrls::Frame3D(Canvas, CellRect, TColor(clBtnShadow), TColor(clBtnHighlight), 2);
  64.     }
  65.     else
  66.       Canvas->Pen->Color = TColor(clBlack);
  67.       TPaletteEntry temp = FPaletteEntries[PalIndex];
  68.       Canvas->Brush->Color = TColor(RGB( temp.peRed, temp.peGreen, temp.peBlue));
  69.       if( Ctl3D )
  70.         Canvas->Pen->Color = TColor(RGB(temp.peRed, temp.peGreen, temp.peBlue));
  71.     if( !ShowSelector )
  72.       Canvas->Rectangle(RECT(CellRect).left,
  73.                         RECT(CellRect).top,
  74.                         RECT(CellRect).right,
  75.                         RECT(CellRect).bottom);
  76.     else
  77.     {
  78.       if(Ctl3D)
  79.       {
  80.         Canvas->Rectangle(RECT(CellRect).left,
  81.                           RECT(CellRect).top,
  82.                           RECT(CellRect).right,
  83.                           RECT(CellRect).bottom);
  84.         InflateRect(&RECT(CellRect), -1, -1);
  85.         DrawFocusRect((HDC)Canvas->Handle, &RECT(CellRect));
  86.       }
  87.       else
  88.       {
  89.         Canvas->Pen->Color = clBlack;
  90.         Canvas->Pen->Mode = pmNot;
  91.         Canvas->Rectangle(RECT(CellRect).left,
  92.                           RECT(CellRect).top,
  93.                           RECT(CellRect).right,
  94.                           RECT(CellRect).bottom);
  95.         Canvas->Pen->Mode = pmCopy;
  96.         Canvas->Rectangle(RECT(CellRect).left +2,
  97.                           RECT(CellRect).top +2,
  98.                           RECT(CellRect).right -2,
  99.                           RECT(CellRect).bottom -2);
  100.       }
  101.     }
  102.   }
  103. }
  104.  
  105.  
  106.  
  107.  
  108. int TernaryOp( Boolean Test, Integer ResultTrue, Integer ResultFalse )
  109. {
  110.    int Result;
  111.    if( Test )
  112.      Result = ResultTrue;
  113.    else
  114.      Result = ResultFalse;
  115.    return Result;
  116. }
  117.  
  118.  
  119. #pragma warn -sig
  120. void __fastcall TCColorGrid::DrawFgBg(void)
  121. {
  122.   TPaletteEntry TextColor;
  123.   Integer PalIndex;
  124.   AnsiString TheText;
  125.   Integer OldBkMode;
  126.   TRect R;
  127.  
  128.   OldBkMode = SetBkMode((HDC)Canvas->Handle, TRANSPARENT);
  129.   if( FForegroundEnabled )
  130.   {
  131.     if( (FForegroundIndex == FBackgroundIndex) && FBackgroundEnabled )
  132.       TheText = Consts_SFB;
  133.     else
  134.       TheText = Consts_SFG;
  135.     if( FForegroundIndex < 8 )
  136.       PalIndex = FForegroundIndex;
  137.     else
  138.       PalIndex = FForegroundIndex + 4;
  139.     TextColor = FPaletteEntries[PalIndex];
  140.     TextColor.peRed = TernaryOp(TextColor.peRed >= 0x80, 0, 0xFF);
  141.     TextColor.peGreen = TernaryOp(TextColor.peGreen >= 0x80, 0, 0xFF);
  142.     TextColor.peBlue = TernaryOp(TextColor.peBlue >= 0x80, 0, 0xFF);
  143.     Canvas->Font->Color = TColor(RGB(TextColor.peRed, TextColor.peGreen, TextColor.peBlue));
  144.  
  145.     R.Left = (FForegroundIndex % FNumXSquares) * FCellXSize;
  146.     R.Right = R.Left + FCellXSize;
  147.     R.Top = (FForegroundIndex / FNumXSquares) * FCellYSize;
  148.     R.Bottom = R.Top + FCellYSize;
  149.     DrawText((HDC)Canvas->Handle, PChar(TheText.c_str()), -1, &RECT(R),
  150.              DT_NOCLIP | DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  151.   }
  152.   if( FBackgroundEnabled )
  153.   {
  154.     if( (FForegroundIndex == FBackgroundIndex) && FForegroundEnabled ) 
  155.       TheText = Consts_SFB;
  156.     else
  157.       TheText = Consts_SBG;
  158.     if( FBackgroundIndex < 8 ) 
  159.       PalIndex = FBackgroundIndex;
  160.     else
  161.       PalIndex = FBackgroundIndex + 4;
  162.  
  163.     TextColor = FPaletteEntries[PalIndex];
  164.     TextColor.peRed = TernaryOp(TextColor.peRed >= 0x80, 0, 0xFF);
  165.     TextColor.peGreen = TernaryOp(TextColor.peGreen >= 0x80, 0, 0xFF);
  166.     TextColor.peBlue = TernaryOp(TextColor.peBlue >= 0x80, 0, 0xFF);
  167.     Canvas->Font->Color = TColor(RGB(TextColor.peRed, TextColor.peGreen, TextColor.peBlue));
  168.     R.Left = (FBackgroundIndex % FNumXSquares) * FCellXSize;
  169.     R.Right = R.Left + FCellXSize;
  170.     R.Top = (FBackgroundIndex / FNumXSquares) * FCellYSize;
  171.     R.Bottom = R.Top + FCellYSize;
  172.     DrawText((HDC)Canvas->Handle, PChar(TheText.c_str()), -1, &RECT(R),
  173.              DT_NOCLIP | DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  174.   }
  175.   SetBkMode((HDC)Canvas->Handle, OldBkMode);
  176. }
  177. #pragma warn .sig
  178.  
  179. void __fastcall TCColorGrid::EnableForeground(Boolean Value)
  180. {
  181.   if( FForegroundEnabled == Value ) 
  182.      return ;
  183.   FForegroundEnabled = Value;
  184.   DrawSquare(FForegroundIndex, (FForegroundIndex == FSelection) && FHasFocus);
  185.   DrawFgBg();
  186. }
  187.  
  188.  
  189.  
  190. void __fastcall TCColorGrid::EnableBackground(Boolean Value)
  191. {
  192.   if( FBackgroundEnabled == Value ) 
  193.        return;
  194.   FBackgroundEnabled = Value;
  195.   DrawSquare(FBackgroundIndex, (FBackgroundIndex == FSelection) && FHasFocus);
  196.   DrawFgBg();
  197. }
  198.  
  199.  
  200.  
  201. TColor __fastcall TCColorGrid::GetForegroundColor(void)
  202. {
  203.   Integer PalIndex;
  204.   if( FForegroundIndex < 8 )
  205.     PalIndex = FForegroundIndex;
  206.   else
  207.     PalIndex = FForegroundIndex + 4;
  208.     return TColor(RGB(FPaletteEntries[PalIndex].peRed,
  209.                   FPaletteEntries[PalIndex].peGreen,
  210.                   FPaletteEntries[PalIndex].peBlue) );
  211. }
  212.  
  213.  
  214. TColor __fastcall TCColorGrid::GetBackgroundColor(void)
  215. {
  216.   Integer PalIndex;
  217.  
  218.   if( FBackgroundIndex < 8 ) 
  219.       PalIndex = FBackgroundIndex;
  220.   else PalIndex = FBackgroundIndex + 4;
  221.   return TColor( RGB(FPaletteEntries[PalIndex].peRed,
  222.                  FPaletteEntries[PalIndex].peGreen,
  223.                  FPaletteEntries[PalIndex].peBlue) );
  224. }
  225.  
  226.  
  227.  
  228.  
  229. void __fastcall TCColorGrid::WMSetFocus(TWMSetFocus &Message)
  230. {
  231.   FHasFocus = True;
  232.   DrawSquare(FSelection, True);
  233.   DrawFgBg();
  234. }
  235.  
  236. void __fastcall TCColorGrid::WMKillFocus(TWMKillFocus &Message)
  237. {
  238.   FHasFocus = False;
  239.   DrawSquare(FSelection, False);
  240.   DrawFgBg();
  241. }
  242.  
  243.  
  244.  
  245. void __fastcall TCColorGrid::KeyDown(Word &Key,  TShiftState Shift)
  246. {
  247.   Integer NewSelection;
  248.   Integer Range;
  249.   TCustomControl::KeyDown(Key, Shift);
  250.   NewSelection = FSelection;
  251.   Range = FNumXSquares * FNumYSquares;
  252.   switch( Key )
  253.   {
  254.      case 0x46:
  255.      case 0x66:
  256.         if( !FForegroundEnabled && FClickEnablesColor ) {
  257.           FForegroundEnabled = True;
  258.           DrawSquare(FForegroundIndex, (FForegroundIndex == FSelection) && FHasFocus);
  259.           FForegroundIndex = -1;
  260.         }
  261.         SetForegroundIndex(NewSelection);
  262.         SetSelection(NewSelection);
  263.         Click();
  264.         break;
  265.       case 0x42:
  266.       case 0x62:
  267.           if( !FBackgroundEnabled && FClickEnablesColor ) {
  268.             FBackgroundEnabled = True;
  269.             DrawSquare(FBackgroundIndex, (FBackgroundIndex == FSelection) && FHasFocus);
  270.             FBackgroundIndex = -1;
  271.           }
  272.           SetBackgroundIndex(NewSelection);
  273.           SetSelection(NewSelection);
  274.           Click();
  275.           break;
  276.     case VK_HOME:
  277.         NewSelection = 0;
  278.         break;
  279.     case VK_UP:
  280.       if( FSelection >= FNumXSquares )
  281.         NewSelection = FSelection - FNumXSquares;
  282.       else
  283.         if( FSelection != 0 )
  284.           NewSelection = Range - FNumXSquares + FSelection - 1;
  285.         else
  286.           NewSelection = Range - 1;
  287.       break;
  288.     case VK_LEFT:
  289.        if( FSelection !=  0 )
  290.          NewSelection = FSelection - 1;
  291.        else
  292.          NewSelection = Range - 1;
  293.        break;
  294.     case VK_DOWN:
  295.        if( FSelection + FNumXSquares < Range )
  296.         NewSelection = FSelection + FNumXSquares;
  297.        else
  298.          if( FSelection != Range - 1 )
  299.            NewSelection = FSelection % FNumXSquares + 1;
  300.          else
  301.            NewSelection = 0;
  302.       break;
  303.     case VK_SPACE:
  304.     case VK_RIGHT:
  305.       if( FSelection != Range - 1 )
  306.         NewSelection = FSelection + 1;
  307.       else
  308.         NewSelection = 0;
  309.       break;
  310.     case VK_END:
  311.        NewSelection = Range - 1;
  312.         break;
  313.     default:
  314.     TCustomControl::KeyDown(Key, Shift);
  315.     return;
  316.   }  //end switch
  317.   Key = 0;
  318.   if( FSelection != NewSelection )
  319.      SetSelection(NewSelection);
  320.  }
  321.  
  322.  
  323. void __fastcall TCColorGrid::WMGetDlgCode(TWMNoParams &Message)
  324. {
  325.   Message.Result = DLGC_WANTARROWS + DLGC_WANTCHARS;
  326. }
  327.  
  328.  
  329.  
  330. void __fastcall TCColorGrid::WMSize(TWMSize &Message)
  331. {
  332.   UpdateCellSizes(False);
  333. }
  334.  
  335. void __fastcall TCColorGrid::CMCtl3DChanged(TMessage &Message)
  336. {
  337.   Invalidate();
  338. }
  339.  
  340. void __fastcall TCColorGrid::MouseDown(TMouseButton Button,  TShiftState Shift, Integer X, Integer Y)
  341. {
  342.   Integer Square;
  343.   TCustomControl::MouseDown(Button, Shift, X, Y);
  344.   FButton = Button;
  345.   FButtonDown = True;
  346.   Square = SquareFromPos(X, Y);
  347.   if( Button == mbLeft ){
  348.     if( !FForegroundEnabled && FClickEnablesColor ){
  349.       FForegroundEnabled = True;
  350.       DrawSquare(FForegroundIndex, (FForegroundIndex == FSelection) && FHasFocus );
  351.       FForegroundIndex = -1;
  352.     }
  353.     SetForegroundIndex(Square);
  354.   }
  355.   else{
  356.     MouseCapture = True;
  357.     if( !FBackgroundEnabled && FClickEnablesColor ) {
  358.       FBackgroundEnabled = True;
  359.       DrawSquare(FBackgroundIndex, (FBackgroundIndex == FSelection) && FHasFocus);
  360.       FBackgroundIndex = -1;
  361.     }
  362.     SetBackgroundIndex(Square);
  363.   }
  364.   SetSelection(Square);
  365.   if( TabStop )
  366.       SetFocus();
  367. }
  368.  
  369.  
  370. void __fastcall TCColorGrid::MouseMove( TShiftState Shift, Integer X, Integer Y)
  371. {
  372.   Integer Square;
  373.  
  374.   TCustomControl::MouseMove(Shift, X, Y);
  375.   if( FButtonDown ){
  376.     Square = SquareFromPos(X, Y);
  377.     if( FButton == mbLeft )
  378.       SetForegroundIndex(Square);
  379.     else
  380.       SetBackgroundIndex(Square);
  381.     SetSelection(Square);
  382.   }
  383. }
  384.  
  385.  
  386. void __fastcall TCColorGrid::MouseUp(TMouseButton Button,  TShiftState Shift, Integer X, Integer Y)
  387. {
  388.   TCustomControl::MouseUp(Button, Shift, X, Y);
  389.   FButtonDown = False;
  390.   if( FButton == mbRight )
  391.      MouseCapture = False;
  392.  }
  393.  
  394.  
  395.  
  396. void __fastcall TCColorGrid::Paint(void)
  397. {
  398.  Integer Row, Col, wEntryIndex;
  399.  Canvas->Font = Font;
  400.  for( Row = 0; Row < FNumYSquares; Row++ )
  401.       for( Col = 0; Col < FNumXSquares; Col++ )
  402.       {
  403.        wEntryIndex = Row * FNumXSquares + Col;
  404.        DrawSquare(wEntryIndex, False);
  405.       }
  406.   DrawSquare(FSelection, FHasFocus);
  407.   DrawFgBg();
  408. }
  409.  
  410.  
  411. void __fastcall TCColorGrid::SetBackgroundIndex(Integer Value)
  412. {
  413.   if( (FBackgroundIndex != Value) && FBackgroundEnabled )
  414.   {
  415.     DrawSquare(FBackgroundIndex, (FBackgroundIndex == FSelection) && FHasFocus);
  416.     FBackgroundIndex = Value;
  417.     if( FBackgroundIndex == FForegroundIndex )
  418.       DrawSquare(FBackgroundIndex, (FBackgroundIndex == FSelection) && FHasFocus);
  419.     DrawFgBg();
  420.     Change();
  421.   }
  422. }
  423.  
  424.  
  425.  
  426. void __fastcall TCColorGrid::SetForegroundIndex(Integer Value)
  427. {
  428.   if( (FForegroundIndex != Value) && FForegroundEnabled  )
  429.   {
  430.     DrawSquare(FForegroundIndex, (FForegroundIndex == FSelection) && FHasFocus);
  431.     FForegroundIndex = Value;
  432.     if( FForegroundIndex == FBackgroundIndex )
  433.       DrawSquare(FForegroundIndex, (FForegroundIndex == FSelection) && FHasFocus );
  434.     DrawFgBg();
  435.     Change();
  436.   }
  437. }
  438.  
  439.  
  440. void __fastcall TCColorGrid::SetGridOrdering(TGridOrdering Value)
  441. {
  442.   if( FGridOrdering == Value  )
  443.        return;
  444.   FGridOrdering = Value;
  445.   FNumXSquares = 16 >> FGridOrdering;
  446.   FNumYSquares = 1 << FGridOrdering;
  447.   UpdateCellSizes(True);
  448. }
  449.  
  450. void __fastcall TCColorGrid::SetSelection(Integer Value)
  451. {
  452.   if( FSelection == Value )
  453.     return;
  454.   DrawSquare(FSelection, False);
  455.   FSelection = Value;
  456.   DrawSquare(FSelection, FHasFocus);
  457.   DrawFgBg();
  458. }
  459.  
  460.  
  461. Integer __fastcall TCColorGrid::SquareFromPos(Integer X, Integer Y)
  462. {
  463.   if( X > Width - 1 )
  464.      X = Width - 1;
  465.   else
  466.     if( X < 0 )
  467.       X = 0;
  468.   if( Y > Height - 1 )
  469.    Y = Height - 1;
  470.   else
  471.      if( Y < 0 )
  472.       Y = 0;
  473.   return( Y / FCellYSize * FNumXSquares + X / FCellXSize);
  474. }
  475.  
  476.  
  477. void __fastcall TCColorGrid::UpdateCellSizes(Boolean DoRepaint)
  478. {
  479.   Integer NewWidth, NewHeight;
  480.   NewWidth = (Width / FNumXSquares) * FNumXSquares;
  481.   NewHeight = (Height / FNumYSquares) * FNumYSquares;
  482.   BoundsRect = Bounds(Left, Top, NewWidth, NewHeight);
  483.   FCellXSize = Width / FNumXSquares;
  484.   FCellYSize = Height / FNumYSquares;
  485.   if( DoRepaint )
  486.     Invalidate();
  487. }
  488.  
  489.  
  490. void __fastcall TCColorGrid::Change(void)
  491. {
  492.   if( FOnChange != NULL )
  493.      FOnChange(this);
  494. }
  495.  
  496.  
  497.